$(function(){
    var countDown = $('#countDown'); //倒计时计数
    var countdownBg = $('.countdown-bg');//倒计时页面
    var countdownBox = $('.countdown-box');//页面二倒计时
    var countDownBtn = $('#countDownBtn'); // 开始按钮
    var chophandIndex = $('.chophand-index'); // 首页
    var gameTime = $('#gameTime');//游戏上面时间
    var startCountDownNum = 4; // 游戏开始倒计时数
    var gameTimeNum = 61; //游戏时间倒计时数
    var gameBox = $('#gameBox');
    var phoneArr = [];
    var nodephoneParent = $('#gameBox');
    var gameY;
    var gameNumber = $('#gameNumber');
    var gameTimey = null;  //游戏时间计时器
//    点击开始按钮
    countDownBtn.on('click',function(){
        chophandIndex.hide();
        countdownBox.show();
        startCountDown();//页面二倒计时开始
    });
//    游戏开始倒计时
    var startCountDown = function(){
        var s = setTimeout(startCountDown,1000);
        startCountDownNum --;
        if(startCountDownNum >= 0){
            countDown.html(startCountDownNum);
        }else{
            clearTimeout(s);
            countdownBg.hide();
            gameTimeShow();//游戏开始时间函数
            foundPhone();//下单函数
        }
    }
//    游戏时间倒计时
    var gameTimeShow = function(){
        gameTimey = setTimeout(gameTimeShow,1000);
        gameTimeNum -- ;
        if(gameTimeNum >= 0){
            gameTime.html(gameTimeNum);
        }else{
            clearTimeout(gameTimey);
            clearTimeout(gameY);
            if(gameNum > 3){
               $('#ejectSuccess').show();    
            }else{
                $('#ejectFail').show();
            }
        }
    }
    
//    生成下单图片
    var phoneNums = 0;
    var foundPhone = function(){
        gameY = setTimeout(foundPhone,5);
        phoneNums ++;
        if(phoneNums == 330){
            var left = Math.random() * (750 - 193) / 100;
            var src = 'img/phone.png';
            phoneArr.push(new foundPhoneShow(1.8,left,nodephoneParent,src));
            phoneNums = 0;
        }
        for(var i = 0; i < phoneArr.length; i ++){
            phoneArr[i].move();
            if(parseInt(phoneArr[i].node.css('top')) >= $(window).height()){
                phoneArr[i].node.remove();
                phoneArr.splice(i,1);
            }
        }
    }
    
//    创建下单图片对象
    var gameNum = 0;
    var foundPhoneShow = function(top,left,nodeParent,src){
        this.src = src;
        this.top = top;
        this.left = left;
        this.nodeParent = nodeParent;
        this.position = 'absolute';
        this.src = src;
        this.node = null;
        this.init = function(){
            this.node = $('<div class="item">');
            this.node.css('position',this.position);
            this.node.css('top',this.top + 'rem');
            this.node.css('left',this.left + 'rem');

            var phone = document.createElement('img');
            phone.src = 'img/duoshow-phone_03.png';
            phone.className = 'phone';
            var left = document.createElement('img');
            left.src = 'img/duoshow-left_03.png';
            left.className = 'wing left';
            var right = document.createElement('img');
            right.src = 'img/duoshow-right_03.png';
            right.className = 'wing right';
            this.node.append(phone);
            this.node.append(left);
            this.node.append(right);
            
            this.nodeParent.append(this.node);
        }
        this.init();
        var _this = this;
        this.node.on('click',function(e){
            
            let el = e.target.parentNode;
            if($(el).hasClass('on')){
                return;
            }
            $(el).addClass('on1 on');
            gameNum ++;
//            _this.node.attr('src','img/phoneB.png');
            gameNumber.html(gameNum);
            $('#frequency').text(gameNum);
            setTimeout(function(){
                 _this.node.remove()
            },900)
            
        })
        this.move = function(){
            this.node.css('top',parseInt(this.node.css('top')) + 10 + 'px')
        }
    }
    
    
    
    var choppShow = function(){
         $('#ejectFail').hide();
         $('#ejectSuccess').hide();
         $('.wicket').hide();
         gameTimeNum = 61;
         gameNum = 0;
         gameNumber.html(gameNum);
         startCountDown();
    }

    $('#choppBtn').on('click',function(){
        choppShow();
    });
    $('#choopbtn').on('click',function(){
        choppShow();
    });
    
    $('.comeBack').on('click',function(){
        choppShow();
    })
    
    
    $('#choujbtn').on('click',function(){
        $('#lottery').show();
    });
    
    $('.close').on('click',function(){
        $('#lottery').hide();
    });
    
  
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
});